#!/bin/bash
#description: configure nfsserver on centos7.4
USER="www"
USERID=2005
GROUPID=2005
SHAREDIR=$1
CLIENTIP="10.45.29.216"
#安装rpc和nfs服务
rpm -qa rpcbind nfs-utils
if [ $? -eq 0 ];then
	yum -y install nfs-utils rpcbind
fi
#配置firewalld防火墙，放行111和892端口
echo "MOUNTD_PORT=892" >/etc/sysconfig/nfs
for port in 892 111;do
	firewall-cmd --add-port=${port}/udp  --permanent 
	firewall-cmd --add-port=${port}/tcp  --permanent 
	firewall-cmd --reload
done

#判断用户www是否存在
if ! id $USER &>/dev/null;then
	groupadd -g $GROUPID $USER
	useradd -u $USERID -g $USER -s /sbin/nologin $USER
fi

#判断需要共享的目录是否存在
[ ! -d $SHAREDIR ] && mkdir -p $SHAREDIR
chown -R $USER $SHAREDIR

#配置共享目录
cat >>/etc/exports <<EOF
#shared /data/video for pet project at 20180412
${SHAREDIR} $CLIENTIP(rw,sync,anonuid=$USERID,anongid=$GROUPID)
EOF

#启动服务
systemctl enable rpcbind
systemctl enable nfs
systemctl start rpcbind
systemctl start nfs

